import Vue from 'vue'
import Router from 'vue-router'

Vue.use(Router)

let kejianrouter = new Router({
  routes: [{
    path: '/',
    name: 'home',
    component: () => import('./views/Home.vue')
  }, {
    path: '/introduce',
    name: 'introduce',
    component: () => import('./views/introduce.vue')
  }, {
    path: '/review',
    name: 'review',
    component: () => import('./views/review.vue')
  }, {
    path: '/current',
    name: 'current',
    component: () => import('./views/current.vue')
  }, {
    path: '/about',
    name: 'about',
    component: () => import('./views/about.vue')
  }, {
    path: '/mine',
    name: 'mine',
    component: () => import('./views/mine.vue')
  }, {
    path: '/apply',
    name: 'apply',
    component: () => import('./views/apply.vue')
  }, {
    path: '/worksDetail',
    name: 'worksDetail',
    component: () => import('./views/detail/worksDetail.vue')
  }, {
    path: '/resultDetail',
    name: 'resultDetail',
    component: () => import('./views/detail/resultDetail.vue')
  }]
})

// 判断是否需要登录权限 以及是否登录
kejianrouter.beforeEach((to, from, next) => {
  // 判断是否需要登录权限
  if (to.matched.some(res => res.meta.requireAuth)) {
    // 判断是否登录
    if (sessionStorage.getItem('token')) {
      next()
    } else {
      // 没登录则跳转到登录界面
      next({
        path: '/login',
        query: {
          redirect: to.fullPath
        }
      })
    }
  } else {
    next()
  }
})

kejianrouter.afterEach(() => {
  window.scrollTo(0, 0)
})

export default kejianrouter